<?php

  /*

      TravianWAP
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/travianwap/
      Contact: uroskn@gmail.com

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU  General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU  General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

  */

  function __printer_shutdown()
  {
    if (defined("PRINTER_WML")) echo "</p></card></wml>";
    else echo "</body></html>";
  }

  // Filter is here for backward compatibility (URLs in TravianWAP)
  function print_link($url, $title, $after = NULL, $filter = False)
  {
    if ($filter) $url = htmlspecialchars($url);
    if (defined("PRINTER_WML")) echo '<anchor>'.$title.'<go href="'.$url.'"/></anchor>'.$after;
    else echo '<a href="'.$url.'">'.$title.'</a>'.$after;
  }

  function print_form_top($action, $hidden_fields = NULL, $method = "post")
  {
    global $__tprinter_fields;
    if (!defined("PRINTER_WML"))
    {
      echo '<form method="'.$method.'" action="'.$action.'">';
      foreach ($hidden_fields as $key => $value)
      {
        if (substr($value, 0, 2) != '$(') echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
      }
    }
    else $__tprinter_fields = array($action, $hidden_fields, $method);
  }

  function print_form_bottom($submit_title, $submit_name = NULL)
  {
    global $__tprinter_fields;
    if (defined("PRINTER_WML"))
    {
      echo '<anchor>'.$submit_title.'<go href="'.$__tprinter_fields[0].'" method="'.$__tprinter_fields[2].'">';
      foreach ($__tprinter_fields[1] as $key => $value) echo '<postfield name="'.$key.'" value="'.$value.'"/>';
      echo '</go></anchor><br/><br/>';
    }
    else echo '<input type="submit" name="'.$submit_name.'" value="'.$submit_title.'"></form>';
    unset($__tprinter_fields);
  }

  // Init routine
  ob_start("ob_gzhandler");
  if (strpos($_SERVER["HTTP_ACCEPT"], "/vnd.wap.wml") !== False)
  {
    define("PRINTER_WML", 1);
    header("Content-Type: text/vnd.wap.wml; charset=utf-8");
    echo '<?xml version="1.0"?><!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"'.
         "\r\n".'"http://www.wapforum.org/DTD/wml_1.1.xml"><wml><card id="TravianWAP" title="TravianWAP"><p>';
  }
  else
  {
    header("Content-Type: text/html; charset=utf-8");
    echo '<html><head><title>TravianWAP</title></head><body>';
  }
  register_shutdown_function("__printer_shutdown");

